package top.caoayu.common.exception;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.caoayu.common.response.Result;

import javax.validation.ValidationException;
import java.util.Arrays;

@RestControllerAdvice
public class GlobalException {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handleException(Exception ex){
        System.out.println(Arrays.toString(ex.getStackTrace()));
        return ResponseEntity.badRequest().body(
                Result.fail(ex.getMessage())
        );
    }
    @ExceptionHandler(ValidationException.class)
    public ResponseEntity<Object> handleValidationException(ValidationException ex) {
        // 处理特定类型的异常逻辑
        // 比如记录日志、返回自定义错误信息等
        return ResponseEntity.badRequest().body(
                Result.fail(ex.getMessage())
        );
    }
}
